<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@include file="/wx/page/session.jsp"%>
<c:set var="userId" value="${param.userId}" />
<c:set var="deptId" value="${param.deptId}" />
<c:set var="deptCode" value="${param.deptCode}" />
<c:set var="deptParentId" value="${param.deptParentId}" />
<c:set var="staffType" value="${param.staffType}" />
<c:set var="taskId" value="${param.taskId}" />
<c:set var="taskType" value="${param.taskType}" />
<c:set var="senderUserId" value="${param.senderUserId}" />
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no, width=device-width">
    <meta content="yes" name="apple-mobile-web-app-capable">
    <meta content="black" name="apple-mobile-web-app-status-bar-style">
    <title>任务管理</title>
    <link rel="stylesheet" href="${ctx}/wx/css/default.css"/>
    <link rel="stylesheet" href="${ctx}/wx/css/picker.css"/>
    <script type="text/javascript" src="${ctx}/wx/js/picker.min.js"></script>
    <script type="text/javascript" src="${ctx}/wx/js/picker.data.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/engine.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/util.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/interface/TaskService.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/interface/UserService.js"></script>
    <script type="text/javascript" src="${ctx}/wx/js/alert.js"></script>
</head>
<body>
<form id="taskForm" name="taskForm" method="post">
    <fieldset class="contact-inner">
        <input type="hidden" id="deptId" name="deptId" value="${deptId}" placeholder="信息部" required>
        <input type="hidden" id="taskId" name="taskId" value="${taskId}">
        <input type="hidden" id="taskState" name="taskState" value="0">
        <input type="hidden" id="userId" name="userId" value="${userId}">
        <input type="hidden" id="taskSenderUserId" name="taskSenderUserId">
        <p class="contact-input">
            <c:if test="${staffType == 1}">
                <input type="radio" id="taskType1" name="taskType" style="width: 20px; height: 20px;" value="1" onclick="setTaskType(1)">
                <label for="taskType1" style="width: 20px">煤贸</label>
            </c:if>
            <input type="radio" id="taskType2" name="taskType" style="width: 20px; height: 20px;" value="2" onclick="setTaskType(2)">
            <label for="taskType2" style="width: 20px">运输</label>
        </p>
        <p class="contact-input">
            <input type="checkbox" id="taskPublishState" name="taskPublishState" style="width: 20px; height: 20px;" checked>
            <label for="taskPublishState" style="width: 20px">发布到任务大厅</label>
        </p>
        <p class="contact-input">
            <input type="search" id="taskSenderUserName" name="taskSenderUserName" placeholder="必填 货主姓名" minlength="2" required readonly>
        </p>
        <p class="contact-input">
            <input type="tel" id="taskSenderUserMobile" name="taskSenderUserMobile" placeholder="必填 货主电话" maxlength="11" required readonly>
        </p>
        <p class="contact-input">
            <select id="taskGoodsType" name="taskGoodsType" style="width: 23%" required>
                <option value="煤炭">煤炭</option>
            </select>
            <select id="taskGoodsStandard" name="taskGoodsStandard" style="width: 75%;" required>
                <option value="沫煤">沫煤</option>
                <option value="籽煤">籽煤</option>
                <option value="六九块">六九块</option>
                <option value="八零块">八零块</option>
                <option value="中块">中块</option>
                <option value="狗头块">狗头块</option>
                <option value="大块">大块</option>
            </select>
        </p>
        <p class="contact-input">
            <input type="tel" id="taskFreightPrice" name="taskFreightPrice" placeholder="运费单价" required>
        </p>
        <p id="taskPurchasePriceP" class="contact-input">
            <input type="tel" id="taskPurchasePrice" name="taskPurchasePrice" placeholder="采购单价">
        </p>
        <p id="taskSalePriceP" class="contact-input">
            <input type="tel" id="taskSalePrice" name="taskSalePrice" placeholder="销售单价">
        </p>
        <p class="contact-input">
            <select id="taskPayWay" name="taskPayWay" required>
                <option value="">运费支付方式</option>
                <option value="现金" selected>现金</option>
                <option value="转账">转账</option>
            </select>
        </p>
        <!--p class="contact-input">
            <select id="taskInsurance" name="taskInsurance">
                <option value="否">购买货运保险</option>
                <option value="是">是</option>
                <option value="否">否</option>
            </select>
        </p>
        <p class="contact-input">
            <select id="taskPremium" name="taskPremium">
                <option value="">保额</option>
                <option value="2">4元保2万</option>
                <option value="3">6元保3万</option>
                <option value="4">8元保4万</option>
            </select>
        </p-->
        <p class="contact-input">
            <input type="search" id="taskLoadingArea" name="taskLoadingArea" placeholder="必填 装货省、市、区" onfocus="picker3(this)" required readonly />
            <br>
            <input type="search" id="taskLoadingAddress" name="taskLoadingAddress" placeholder="必填 装货地点" required />
        </p>
        <p class="contact-input">
            <input type="search" id="taskUnloadingArea" name="taskUnloadingArea" placeholder="必填 卸货省、市、区" onfocus="picker3(this)"  required readonly />
            <br>
            <input type="search" id="taskUnloadingAddress" name="taskUnloadingAddress" placeholder="必填 卸货地点" required />
        </p>
        <p class="contact-input">
            <input type="tel" id="taskTruckTotalNum" name="taskTruckTotalNum" placeholder="必填 需要车辆" required>
        </p>
        <p class="contact-input">
            <input type="tel" id="taskCostPrice" name="taskCostPrice" placeholder="必填 信息费" required>
        </p>
        <p class="contact-input">
            <input type="search" id="taskRemark" name="taskRemark" placeholder="备注">
        </p>
        <p class="contact-submit">
            <input type="button" value="保存任务" onclick="submitTask()">
        </p>
    </fieldset>
</form>
</body>
</html>
<script type="text/javascript">
    function picker3(input) {
        new Picker({
            //"title": '请选择',//标题(可选)
            "defaultValue": input.value,//默认值-多个以空格分开（可选）
            "data": cityData,//数据(必传)
            "keys": {
                "id": "Code",
                "value": "Name",
                "childData": "level"//最多3级联动
            },//数组内的键名称(必传，id、text、data)
            "callBack": function (value, id) {
                //回调函数（val为选择的值）
                input.value = value;
            }
        });
    }
    if("${taskId}".length > 0) {
        $("taskCostPrice").disabled = true;
        var task = new Task();
        task.deptId = "${deptId}";
        task.taskId = "${taskId}";
        TaskService.selectTaskList(task, callbackSelectTaskList);
    } else {
        <c:if test="${empty taskType}">
            <c:if test="${staffType == 1}">
            document.getElementById("taskType1").click();
            </c:if>
            <c:if test="${staffType == 0}">
            document.getElementById("taskType2").click();
            </c:if>
        </c:if>
        <c:if test="${!empty taskType}">
            document.getElementById("taskType${taskType}").click();
        </c:if>
    }
    function callbackSelectTaskList(list) {
        var task = list[0];
        dwr.util.setValues(task);
        var taskType = task.taskType;
        setTaskType(taskType);
    }
    if("${senderUserId}".length > 0) {
        var sender = new Sender();
        sender.userId = "${senderUserId}";
        UserService.selectSenderList(sender, callbackSelectSenderList);
    }
    function callbackSelectSenderList(list) {
        var sender = list[0];
        dwr.util.setValue("taskSenderUserId", sender.userId);
        dwr.util.setValue("taskSenderUserName", sender.user.userName);
        dwr.util.setValue("taskSenderUserMobile", sender.user.userMobile);
        dwr.util.setValue("taskGoodsStandard", sender.senderLastGood);
        var senderLastDest = sender.senderLastDest;
        if(senderLastDest != null) {
            var array = senderLastDest.split("-");
            if(array.length > 0) {
                dwr.util.setValue("taskUnloadingArea", array[0]);
                if (array.length > 1) {
                    dwr.util.setValue("taskUnloadingAddress", array[1]);
                }
            }
        }
    }
    function setTaskType(taskType) {
        if(taskType == 1) {
            $("taskSalePriceP").style.display = "block";
            $("taskPurchasePriceP").style.display = "block";
            selectStaff();
            $("taskSenderUserName").onclick = function () {

            }
        } else if(taskType == 2) {
            if("${taskId}".length == 0) {
                dwr.util.setValue("taskSenderUserId", "");
                dwr.util.setValue("taskSenderUserName", "");
                dwr.util.setValue("taskSenderUserMobile", "");
            }
            dwr.util.setValue("taskSalePrice", "");
            dwr.util.setValue("taskPurchasePrice", "");
            $("taskSalePriceP").style.display = "none";
            $("taskPurchasePriceP").style.display = "none";
            $("taskSenderUserName").onclick = function () {
                location = "${ctx}/wx/page/task_sender_list.jsp?userId=${userId}&deptId=${deptId}&deptCode=${deptCode}&deptParentId=${deptParentId}&staffType=${staffType}&taskId=${taskId}&taskType=2";
            }
        }
    }
    function selectStaff() {
        var staff = new Staff();
        staff.deptId = "${deptId}";
        staff.staffType = 1;
        UserService.selectStaffList(staff, callbackSelectStaffList);
    }
    function callbackSelectStaffList(list) {
        if (list.length > 0) {
            var staff = list[0];
            var user = staff.user;
            dwr.util.setValue("taskSenderUserId", user.userId);
            dwr.util.setValue("taskSenderUserName", user.userName);
            dwr.util.setValue("taskSenderUserMobile", user.userMobile);
        } else {
            alert("该信息部还未添加联系人");
            callback();
        }
    }
    function submitTask() {
        if (validate($("taskForm"))) {
            if("${taskId}".length > 0) {
                updateTask();
            } else {
                insertTask();
            }
        }
    }
    function insertTask() {
        if(confirm("任务添加后将无法修改")){
            var task = dwr.util.getFormValues("taskForm");
            task.taskTruckOccupyNum = 0;
            TaskService.insertTask(task, callback);
        }
    }
    function updateTask() {
        var task = dwr.util.getFormValues("taskForm");
        TaskService.updateTask(task, callback);
    }
    function callback() {
        location = "${ctx}/wx/page/task_list.jsp?userId=${userId}&deptId=${deptId}&deptCode=${deptCode}&deptParentId=${deptParentId}";
    }
</script>